package org.elec.mac.inter;

import com.sun.org.apache.bcel.internal.classfile.Unknown;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.elec.mac.entity.OAuthToken;
import org.elec.mac.entity.SCategory;
import org.elec.mac.entity.SClientWx;
import org.elec.mac.exception.ErrorCode;
import org.elec.mac.exception.ErrorMsg;
import org.elec.mac.service.SCategoryService;
import org.elec.mac.service.SClientWxService;
import org.elec.mac.service.SMachineMenuService;
import org.elec.mac.utils.PriceUtil;
import org.elec.mac.utils.Utils;
import org.elec.mac.utils.WebKit;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Slf4j
@Controller
@RequestMapping(value = "/menu")
public class IntMenuController extends InterController {

    @Autowired
    private SMachineMenuService sMachineMenuService;

    @Autowired
    private SCategoryService sCategoryService;

    @Value("${annex.image.url}")
    private String imageurl;

    @RequestMapping(value = "/intmenu.do")
    private void intMenuHandler(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int code = ErrorCode.SUCCESS;
        String msg = ErrorMsg.errorMessage(code);
        JSONObject body = new JSONObject();
        try {
            String machserial = request.getParameter("machserial");

            Map<String, Object> params = new HashMap<>();
            params.put("machid", Utils.parseLong(machserial, 0L));
            List<Map<String, Object>> sMachineMenuList = sMachineMenuService.getMachineMenuList(params);
            params.clear();
            params.put("flag",1);
            List<SCategory> sCategories = sCategoryService.list(params);
            if(sMachineMenuList != null && sMachineMenuList.size() > 0) {
                JSONArray jsonArray = new JSONArray();
                for(Map<String, Object> sMachineMenu : sMachineMenuList) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("machserial", sMachineMenu.get("machid"));
                    jsonObject.put("pid", sMachineMenu.get("pid"));
                    jsonObject.put("productname", sMachineMenu.get("productname"));
                    jsonObject.put("description", sMachineMenu.get("description"));
                    jsonObject.put("price", PriceUtil.toPriceString(Utils.parseLong(sMachineMenu.get("price").toString(),0)));
                    jsonObject.put("cprice", PriceUtil.toPriceString(Utils.parseLong(sMachineMenu.get("cprice").toString(),0)));
                    jsonObject.put("imageurl", imageurl+sMachineMenu.get("picpath"));
                    jsonObject.put("maxcup", sMachineMenu.get("maxcup"));
                    JSONArray materials = new JSONArray();
                    String[] str = sMachineMenu.get("materials").toString().split(",");
                    for (String i : str) {
                        materials.put(Utils.parseInt(i,0));
                    }
                    jsonObject.put("materials", materials);
                    jsonObject.put("cid", sMachineMenu.get("cid"));
                    jsonObject.put("cname", sMachineMenu.get("cname"));
                    jsonArray.put(jsonObject);
                }
                body.put("total", sMachineMenuList.size());
                body.put("list", jsonArray);
            }
            if(sCategories != null && sCategories.size() > 0) {
                body.put("clist",sCategories);
            }

        } catch(Exception ex) {
            log.error("菜单获取失败", ex);
            code = ErrorCode.ERR_FAILED;
            msg = "[" + code + "]菜单获取失败";
        }
        this.responseJson(response, code, msg, body);
    }
}
